<!-- 我的 - 登录页 -->
<template>
  <view class="min-h-screen bg-home">
    <u-navbar title="" :background="{ background: 'linear-gradient(0deg, #fffbee 0%, #faf9f7 100%)' }"
      :border-bottom="false" :custom-back="doBack" />
    <view class="flex flex-col items-center">
      <image class="w-326 h-260 mt-15" src="/static/images/common/img_logo.png"></image>
      <button v-if="state.isAgree" class="btn-login u-reset-button" open-type="getPhoneNumber"
        @getphonenumber="onGetPhoneClick">授权手机号一键登录</button>
      <view v-else class="btn-login" @click="onGetPhoneClick">授权手机号一键登录</view>
      <view class="flex items-center h-50" @click="state.isAgree = !state.isAgree">
        <view class="flex justify-center items-center h-50">
          <image v-if="state.isAgree" class="w-28 h-28" src="/static/images/common/ic_agree.png"></image>
          <view v-else class="circle-agree rounded-full" />
        </view>
        <view class="c-#999 text-26 ml-1">我已阅读并同意<text class="c-#6492E4"
            @click.stop="navigate('/package_user/pages/common/rule?title=用户服务隐私协议&type=userRule')">《用户服务隐私协议》</text>
        </view>
      </view>
    </view>
    <z-privacy color="#F4AF22" :onNeed='false' :hideTabBar='true'/>
  </view>
</template>

<script setup>
import { navigate } from "@/common/util/uni";

import { useUserStore } from '@/store/user'
import { showModal } from '@/common/util/uni'
import { postSocietyInvite } from '@/common/http/module/society.js'

const userStore = useUserStore()

const state = reactive({
  sceneArray: [], // 扫码进入
  isAgree: false
})

function onGetPhoneClick (e) {
  if (!state.isAgree) {
    showModal({
      content: '请先阅读并同意《用户服务隐私协议》',
    })
    return
  }
  const { code } = e.detail
  if (code) {
    userStore.login(code).then(() => {
      uni.showToast({
        title: '登录成功',
      })
      // 调用加入学社接口
      if (state.sceneArray[0] == 1 && state.sceneArray[2]) {
        postSocietyInvite({ club_id: state.sceneArray[3], invite_code: state.sceneArray[2] }).then(res => {
          console.log('邀请', res.data.result);
          setTimeout(() => doBack(), 500)
        })
      } else {
        setTimeout(() => doBack(), 500)
      }
    })
  } else {
    uni.showToast({
      icon: 'none',
      title: '登录失败，小程序授权参数错误',
    })
  }
}

function doBack () {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const loginPages = pages.filter(item => item.route == currentPage.route)
  uni.navigateBack({
    delta: loginPages.length
  })
}

onLoad(() => {
  // 获取识别小程序的scene值
  try {
    const { query } = wx.getEnterOptionsSync()
    if (query && query.scene) {
      const scene = decodeURIComponent(query.scene)
      state.sceneArray = scene.split("_")
    }
  } catch (e) {
    console.log(e);
  }
  // 扫码新用户绑定上下级
  if (state.sceneArray[1]) {
    userStore.setParentId(state.sceneArray[1])
  }
})
</script>

<style lang="scss" scoped>
.bg-home {
  background: linear-gradient(0deg,
      rgba(246, 246, 246, 1) 0%,
      rgba(249, 249, 249, 1) 50.76%,
      rgba(249, 249, 249, 1) 51%,
      rgba(255, 251, 239, 1) 97.54%,
      rgba(255, 251, 238, 1) 100%);
}

.circle-agree {
  width: 28rpx;
  height: 28rpx;
  border: 4rpx solid #999999;
}

.btn-login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 638rpx;
  height: 88rpx;
  margin-top: 136rpx;
  margin-bottom: 64rpx;
  padding: 26rpx;
  border-radius: 418rpx;
  background-color: #F4AF22;
  color: #fff;
  font-size: 32rpx;
}
</style>
